package com.kuangstudy.service.hyperloglog;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Random;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/5/25 11:46
 */
@Service
@Log4j2
public class UvService {

    @Autowired
    private RedisTemplate redisTemplate;


    // 1:  模拟UV统计
//    @PostConstruct
//    public void initUV() {
//        log.info("模拟UV统计初始化...");
//        new Thread(() -> {
//            toggleEverDay();
//        }).start();
//    }

    //2 : 模拟一个月的访问量
    public void toggleEverDay() {
        while (true) {
            for (int i = 1; i <= 31; i++) {
                Random random = new Random();
                String randomip = random.nextInt(256) + "." + random.nextInt(256) + "." + random.nextInt(256);
                String day = i + "";
                if (i < 10) day = "0" + i;
                log.info("uv:202105" + day + ",访问IP是:{}", randomip);
                redisTemplate.opsForHyperLogLog().add("uv:202105" + day, randomip);
            }
            try {
                Thread.sleep(3000);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
